(function($, doc) {
	mui.init();
	$.plusReady(function() {

		//var mainPage = plus.webview.getWebviewById("main.html");
		//var main_loaded_flag = false;

		var toMain = function() {
			location.href = 'main.html';
			/*
			mui.openWindow({
				id: 'main.html',
				url: 'main.html',
				show: {
					aniShow: "slide-in-right",
					duration: 300,
					autoShow: false
				},
				waiting: {
					title: "正在加载，请稍后..."
				}
			});*/
		};
		document.getElementById('reg').addEventListener('tap', function(event) {
			location.href = 'reg.html';
		});

		localStorage.removeItem('_send_code');
		mui('#pwd_div').on('tap', 'a', function() {
			document.getElementById('pwd_div').style.display = 'none';
			document.getElementById('phone_code_div').style.display = 'block';
			document.getElementById('login_type').value = 2;
		});

		mui('#phone_code_div').on('tap', 'a', function() {
			document.getElementById('pwd_div').style.display = 'block';
			document.getElementById('phone_code_div').style.display = 'none';
			document.getElementById('login_type').value = 1;
		});

		//点击获取验证码
		document.getElementById("getCode").addEventListener('tap', function() {
			localStorage.removeItem('_send_code');
			var c1 = 60,
				phone = document.getElementById('account').value;
			//var btnArray = ['确定', '取消'];
			var btnArray = ['确定', '取消'];
			mui.confirm('我们将发送验证码短信到下面的号码：\n' + phone, '确认手机号码', btnArray, function(e) {
				if(e.index == 0) {
					if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
						plus.nativeUI.toast("手机号码有误，请重填手机号");
					} else {

						mui.get(smsurl, {
							'phone': phone
						}, function(res) {
							if(res.statu == 1) {
								//发送短信成功
								localStorage.setItem('_send_code', res.ck_code);
							} else {
								plus.nativeUI.toast(res.msg);
								document.getElementById('getCode').innerHTML = '获取验证码';
								document.getElementById('getCode').removeAttribute('disabled');
								c1 = 60;
								return;
							}
							var code1 = setInterval(function() {
								document.getElementById('getCode').innerHTML = c1 + '秒后重新获取';
								c1 = c1 - 1;
								if(c1 >= 0) {
									document.getElementById('getCode').setAttribute("disabled", "disabled");
								} else {
									document.getElementById('getCode').innerHTML = '获取验证码';
									document.getElementById('getCode').removeAttribute('disabled');
									c1 = 60;
									clearInterval(code1);
								}
							}, 1000);
						});
					}
				} else {

				}
			})

		});

		//var lastUser = state.token ? state : app.getOneUser();

		document.getElementById('login').addEventListener('tap', function(event) {
			var accountBox = document.getElementById('account');
			var passwordBox = document.getElementById('password');
			var state = app.getState();
			var log_type = document.getElementById('login_type').value;

			var loginInfo = {
				account: accountBox.value,
				password: '',
				code: '',
				ck_code: '',
				login_type: log_type
			};

			if(!(/^1(3|4|5|7|8)\d{9}$/.test(loginInfo.account))) {
				plus.nativeUI.toast("手机号码有误，请重填");
				return;
			}
			if(log_type == '1') {
				loginInfo.password = passwordBox.value;
				if(loginInfo.password.length < 6) {
					plus.nativeUI.toast('密码最短为 6 个字符');
					return;
				}

				loginInfo.passwd = md5(loginInfo.password);
			}

			if(log_type == '2') {
				if(document.getElementById('code').value == '') {
					plus.nativeUI.toast('请填写验证码');
					return;
				}
				if(!localStorage.getItem('_send_code')) {
					plus.nativeUI.toast('请获取验证码');
					return;
				}
				/*
				if(document.getElementById('code').value.trim() != localStorage.getItem('_send_code')){
					mui.alert('验证码错误');
					return ;
				}*/
				loginInfo.code = document.getElementById('code').value.trim();
				loginInfo.ck_code = localStorage.getItem('_send_code');
			}

			$.post(loginurl, loginInfo, function(res) {
				//console.log(res.status);
				//console.log(JSON.stringify(res));
				//return;
				if(res.status < 1) {
					mui.alert(res.msg);

				} else {
					localStorage.removeItem('_send_code');
					app.createState(res.userinfo);
					res.userinfo.pwd = loginInfo.passwd;
					app.setUsers(res.userinfo);
					console.log(res.userinfo.user_id);
					toMain();
				}
			});

		});
	});

	/*
	function toMain() {
		//location.href = 'main.html';
		mui.openWindow({id : 'main.html', url : 'main.html'});
	}*/

}(mui, document));